package com.mall.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.SmsHomeNewProductApi;
import com.mall.pageResult.PageResult;
import com.mall.pojo.SmsHomeBrand;
import com.mall.pojo.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HomeNewProductService {

    @DubboReference
    private SmsHomeNewProductApi homeNewProductApi;

    /**
     * 分页查询首页推荐新品
     *
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    public PageResult<SmsHomeNewProduct> list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        //查询推荐新品分页信息
        Page<SmsHomeNewProduct> page = homeNewProductApi.list(pageNum, pageSize, productName, recommendStatus);
        //组装返回结果数据
        PageResult<SmsHomeNewProduct> result = new PageResult<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(page.getRecords());
        result.setTotal(Convert.toInt(page.getTotal()));
        result.setTotalPage(Convert.toInt(page.getPages()));
        return result;
    }

    /**
     * 批量修改首页推荐新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    public Integer updateRecommendStatus(Long[] ids, Integer recommendStatus) {
        //根据一组id查询推荐新品信息
        List<SmsHomeNewProduct> productList = homeNewProductApi.findProductByIds(ids);
        //修改这组推荐新品状态
        productList = productList.stream().map(brand->{
            brand.setRecommendStatus(recommendStatus);
            return brand;
        }).collect(Collectors.toList());
        //批量修改推荐新品状态
        Integer count = 0;
        for (SmsHomeNewProduct product : productList) {
            homeNewProductApi.updateProduct(product);
            count++;
        }

        return count;
    }

    /**
     * 添加首页新品
     * @param homeNewProductList
     * @return
     */
    public Integer create(List<SmsHomeNewProduct> homeNewProductList) {
        Integer count = 0;
        //判断商品是否已经在推荐列表
        for (SmsHomeNewProduct product : homeNewProductList) {
            Long productId = product.getProductId();
            //根据商品id查询推荐新品信息
            SmsHomeNewProduct homeNewProduct = homeNewProductApi.findProductByProductId(productId);
            if (ObjectUtil.isNull(homeNewProduct)){
                //添加推荐新品
                //设置默认排序和推荐状态
                product.setSort(0);
                product.setRecommendStatus(1);
                homeNewProductApi.create(product);
                count++;
            }
        }
        return count;
    }

    /**
     * 修改首页新品排序
     * @param id
     * @param sort
     * @return
     */
    public Integer updateSort(Integer id, Integer sort) {
        //sort为null给个默认值0
        if (ObjectUtil.isNull(sort)){
            sort = 0;
        }
        //根据id查询推荐新品信息
        SmsHomeNewProduct product = homeNewProductApi.findProductById(id);
        product.setSort(sort);
        Integer count = homeNewProductApi.updateProduct(product);
        return count;
    }

    /**
     * 批量删除首页新品
     * @param ids
     * @return
     */
    public Integer delete(Long[] ids) {
        Integer count = homeNewProductApi.deleteByIds(ids);
        return count;
    }
}
